using System;
using System.Windows.Forms;
using CSW.Framework.Common;
using CSW.Framework.UI.Binding;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace CSW.Framework.UI.DevEx.Binding
{
    /// <summary>
    /// A wrapper class for binding <see cref="DateEdit"/> objects with <see cref="Binder"/>.
    /// </summary>
    public class BindableDateEdit : IBindableControl
    {
        private readonly DateEdit m_DateEdit;

        /// <summary>
        /// Initializes a new instance of the <see cref="BindableDateEdit"/> class.
        /// </summary>
        /// <param name="dateEdit">The <see cref="DateEdit"/> control to wrap.</param>
        /// <param name="addClearButton">if set to <c>true</c> a clear button will be added to the <see cref="DateEdit"/>.</param>
        public BindableDateEdit(DateEdit dateEdit, bool addClearButton)
        {
            Guard.ArgumentNotNull(dateEdit, "dateEdit");

            m_DateEdit = dateEdit;

            m_DateEdit.Validated += DateEdit_Validated;
            m_DateEdit.EditValueChanged += DateEdit_Validated;

            if (addClearButton)
            {
                // Search for butotn
                bool found = false;
                foreach (EditorButton button in dateEdit.Properties.Buttons)
                {
                    if (button.Kind == ButtonPredefines.Delete)
                    {
                        found = true;
                        break;
                    }
                }

                // If not found, add
                if (!found)
                    m_DateEdit.Properties.Buttons.Add(new EditorButton(ButtonPredefines.Delete));

                // Handle click
                m_DateEdit.ButtonClick += BaseEditEventHandlers.DeleteButtonClick;
                m_DateEdit.KeyDown += BaseEditEventHandlers.DeleteKeyDown;
            }
        }

        private void DateEdit_Validated(object sender, EventArgs e)
        {
            EventHandler tmpValidated = Validated;
            if (tmpValidated != null)
            {
                tmpValidated(sender, e);
            }
        }

        #region IBindableControl Members

        /// <summary>
        /// Occurs when the control has validated.
        /// </summary>
        public event EventHandler Validated;

        /// <summary>
        /// Sends the <see cref="Validated"/> event.
        /// </summary>
        public void SendValidated()
        {
            EventHandler validated = Validated;
            if (validated != null)
                validated(this, EventArgs.Empty);
        }

        /// <summary>
        /// Gets the control this object wraps.
        /// </summary>
        /// <value>The control this object wraps.</value>
        public Control Control
        {
            get { return m_DateEdit; }
        }

        /// <summary>
        /// Gets or sets the value of the control.
        /// </summary>
        /// <value>The value of the control.</value>
        public object Value
        {
            get { return m_DateEdit.EditValue; }
            set
            {
                m_DateEdit.EditValue = (DateTime?)value;
                //m_DateEdit.DoValidate();
                //m_DateEdit.ClosePopup();
            }
        }

        /// <summary>
        /// Gets the name of the control.
        /// </summary>
        /// <value>The name of the control.</value>
        public string Name
        {
            get { return m_DateEdit.Name; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the control is enabled.
        /// </summary>
        /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
        public bool Enabled
        {
            get { return m_DateEdit.Enabled; }
            set { m_DateEdit.Enabled = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the control is read only.
        /// </summary>
        /// <value><c>true</c> if read only; otherwise, <c>false</c>.</value>
        public bool ReadOnly
        {
            get { return m_DateEdit.Properties.ReadOnly; }
            set 
            { 
                m_DateEdit.Properties.ReadOnly = value; 
                m_DateEdit.TabStop = !value;

                foreach (EditorButton button in m_DateEdit.Properties.Buttons)
                {
                    button.Enabled = !value;
                }
            }
        }

        /// <summary>
        /// Focuses the control.
        /// </summary>
        public void Focus()
        {
            m_DateEdit.Focus();
        }

        /// <summary>
        /// Gets the tab index.
        /// </summary>
        /// <value>The tab index.</value>
        public int TabIndex
        {
            get { return m_DateEdit.TabIndex; }
        }

        /// <summary>
        /// Gets or sets the maximum string length.
        /// </summary>
        /// <value>The maximum string length.</value>
        public int MaxLength
        {
            get { throw new NotSupportedException("MaxLength is not supported."); }
            set { throw new NotSupportedException("MaxLength is not supported."); }
        }

        #endregion

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            Validated = null;
            
            m_DateEdit.Validated -= DateEdit_Validated;
            m_DateEdit.EditValueChanged -= DateEdit_Validated;
            m_DateEdit.ButtonClick -= BaseEditEventHandlers.DeleteButtonClick;
            m_DateEdit.KeyDown -= BaseEditEventHandlers.DeleteKeyDown;
        }
    }
}